$(document).ready(function () {
	// 表单验证
	$("form").on("submit", function (event) {
		event.preventDefault(); // 阻止表单默认提交

		var id = sessionStorage.getItem(id);
		var token = sessionStorage.getItem(token);

		var oldpassword = $(".oldpassword").val();
		var newpassword = $(".newpassword").val();
		var rnewpassword = $(".rnewpassword").val();

		// 简易的前端表单验证示例
		if (!oldpassword || !newpassword || !rnewpassword) {
			alert("请填写所有必填项");
			return false;
		}
		if (newpassword !== rnewpassword) {
			alert("两次新密码输入不一致");
			return false;
		}

		// 发送修改密码请求
		axios
			.post(
				`http://localhost:9000/users/rpwd`,
				{
					id: sessionStorage.getItem('id'),
					oldPassword: oldpassword,
					newPassword: newpassword,
					rNewPassword: rnewpassword,
				},
				{
					headers: {
						authorization: sessionStorage.getItem("token"),
					},
				}
			)
			.then(function (response) {
				if (response.data.code === 1) {
					alert(response.data.message);
					sessionStorage.clear(); // 清除用户数据
					window.location.href = "./login.html";
				} else {
					alert(response.data.message);
				}
			})
			.catch(function (error) {
				alert("请求出错");
			});
	});
});
